<template>
  <div>
    <!-- 搜索栏区域 -->
    <el-card class="SearchCard" body-style="padding:0px">
      <el-row>
        <el-col :span="2">
          <div class="SearchTitle">条件搜索栏</div>
        </el-col>
        <el-col :span="4">
          <el-row>
            <el-col :span="10">
              <div class="SearchLable">搜索关键字</div>
            </el-col>

            <el-col :span="14">
              <div class="SearchLable">
                <el-input placeholder="请输入内容" v-model="queryInfo.srm" @clear="clearEvent" clearable></el-input>
              </div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" type="primary" @click="getSearchList()">搜索</el-button>
          </div>
        </el-col>
        <el-col :span="2">
          <div class="SearchLable">
            <el-button size="medium" @click="addNewDrugVisible = true" type="primary">添加新药</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="TableCard" :body-style="{ padding: '0px' }">
      <div>
        <el-table
          :data="TableData"
          border
          :header-cell-style="{ 'text-align':'center',background: '#3F9DFD', color: '#FFF' }"
          :cell-style="{'text-align':'center'}"
          size="medium"
          stripe
          style="width: 100%;font-size:12px"
        >
          <el-table-column type="index" width="50"></el-table-column>
          <el-table-column prop="name" label="查看小类" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="Subclass(scope.row.id,scope.row.allerName)"
              >查看小类</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                type="text"
                size="mini"
                @click="showupdateDrug(scope.row.id,scope.row.allerName,scope.row.allerType)"
              >更新成分</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="allerName" label="大类过敏源名称"></el-table-column>
        </el-table>
      </div>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[20, 50, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total"
          background
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="'大类——'+allergenData.allergenName+'——过敏源的小类列表'"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-card class="dialog-card">
        <el-form
          :inline="true"
          :model="allergenData"
          style="margin-left:15px"
          class="demo-form-inline"
        >
          <el-form-item label="搜索关键字">
            <el-input v-model="allergenData.srm" size="mini" placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="dialogSearch()">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <el-card class="dialog-card2" style="margin-top: 12px;height:344px">
        <el-table :data="allergenList" height="280" border size="mini" style="width: 100%">
          <!-- <el-table-column type="selection"
                           width="40">
          </el-table-column>-->
          <el-table-column prop="id" label="过敏源小类编码" width="180"></el-table-column>
          <el-table-column prop="allerName" label="过敏源小类名称"></el-table-column>
        </el-table>
        <div class="block" style="height:32px;margin-top:6px;margin-bottom:6px;float:left;">
          <el-pagination
            @size-change="dialogHandleSizeChange"
            @current-change="dialoghandleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[5, 10, 20]"
            :page-size="allergenData.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="allergenData.total"
            background
          ></el-pagination>
        </div>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加新药"
      :visible.sync="addNewDrugVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-card>
        <el-form label-position="right" label-width="80px" :model="allergenData">
          <el-form-item label="成分名称">
            <el-input v-model="addNewDrugData.allerName" style="width:45%" placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item label="类别">
            <el-select v-model="addNewDrugData.allerType" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="addNewDrug()">新增</el-button>
        <el-button size="mini" @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <!-- 更新////////////////////////////// -->
    <el-dialog
      title="更新药品"
      :visible.sync="updateDrugVisible"
      width="40%"
      :before-close="handleClose"
    >
      <el-card>
        <el-form label-position="right" label-width="80px" :model="allergenData">
          <el-form-item label="成分名称">
            <el-input v-model="updateDrugData.allerName" style="width:45%" placeholder="请输入关键字"></el-input>
          </el-form-item>
          <el-form-item label="类别">
            <el-select v-model="updateDrugData.allerType" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-card>

      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="updateDrug()">更新</el-button>
        <el-button size="mini" @click="updateDrugVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAllergenList,
  getAllergenSubClass,
  addNewDrug,
  updateAllergen
} from "@/api/rad";
export default {
  data() {
    return {
      queryInfo: {
        srm: "",
        pageList: 1,
        pageSize: 20,
        type: 2
      },
      total: 0,
      TableData: [],
      dialogVisible: false,
      allergenList: [],
      allergenData: {
        allergenName: "",
        id: "",
        srm: "",
        pageList: 1,
        pageSize: 10,
        total: 0
      },
      addNewDrugVisible: false,
      addNewDrugData: {
        allerName: "",
        allerType: ""
      },
      options: [
        {
          value: 1,
          label: "小类"
        },
        {
          value: 2,
          label: "大类"
        }
      ],
      updateDrugVisible: false,
      updateDrugData: {
        id: "",
        allerName: "",
        allerType: ""
      }
    };
  },
  created: function() {
    this.getAllergenList();
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      console.log(newSize);
      this.getAllergenList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pageList = newPage;
      console.log(newPage);
      this.getAllergenList();
    },

    dialogHandleSizeChange(newSize) {
      this.allergenData.pageSize = newSize;
      this.getAllergenSubClass();
    },

    dialoghandleCurrentChange(newPage) {
      this.allergenData.pageList = newPage;
      this.getAllergenSubClass();
    },
    clearEvent() {
      this.getAllergenList();
    },
    async getAllergenList() {
      let res = await this.$rad.getAllergenList({
        srm: this.queryInfo.srm,
        pageList: this.queryInfo.pageList,
        pageSize: this.queryInfo.pageSize,
        type: this.queryInfo.type
      });
      this.TableData = res.rows;
      this.total = res.total;
      console.log(res);
    },
    getSearchList() {
      this.getAllergenList();
    },
    async getAllergenSubClass() {
      let res = await this.$rad.getAllergenSubClass({
        groupId: this.allergenData.id,
        srm: this.allergenData.srm,
        page: this.allergenData.pageList,
        pageSize: this.allergenData.pageSize
      });
      this.allergenData.total = res.total;
      this.allergenList = res.rows;
      console.log(res);
    },
    Subclass(id, name) {
      this.allergenData.id = id;
      this.allergenData.allergenName = name;
      this.getAllergenSubClass();
      this.dialogVisible = true;
    },
    async addNewDrug() {
      let res = await this.$rad.addNewDrug({
        allerName: this.addNewDrugData.allerName,
        allerType: this.addNewDrugData.allerType
      });
      console.log(res);
      if (res.code == 1) {
        this.$message.success(res.msg);
        this.addNewDrugData.allerName = "";
        this.addNewDrugData.allerType = "";
        this.addNewDrugVisible = false;
      } else {
        this.$message.error(res.msg);
      }
    },
    dialogSearch() {
      this.getAllergenSubClass();
    },
    showupdateDrug(id, allerName, allerType) {
      this.updateDrugData.id = id;
      this.updateDrugData.allerName = allerName;
      this.updateDrugData.allerType = allerType;
      this.updateDrugVisible = true;
    },
    async updateDrug() {
      let res = await this.$rad.updateAllergen({
        id: this.updateDrugData.id,
        allerName: this.updateDrugData.allerName,
        allerType: this.updateDrugData.allerType
      });
      console.log(res);
      if (res.code == 1) {
        this.$message.success(res.msg);

        this.updateDrugVisible = false;
        this.getAllergenList();
      } else {
        this.$message.error(res.msg);
      }
    }
  }
};
</script>
<style scoped>
.SearchCard {
  height: 50px;
  font-size: 12px;
}
.SearchTitle {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid #c2c2c2;
}
.SearchLable {
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.TableCard {
  margin-left: 3px;
  margin-top: 2px;
}
.block {
  text-align: center;
  margin-top: 12px;
  margin-bottom: 12px;
}
.dialog-card >>> .el-card__body {
  padding: 0px 0px;
  width: 100%;
  height: 40px;
  /* background-color: red; */
}
</style>